import app from "../../../app";
import { EffectCondition, EffectType, Sk_minType } from "../../../shared/interface";
import { SKILL, battle_logNum } from "../skill/SK_active";
import { baseBody } from "./baseBody";

class body_tool {
    constructor() {

    }
    /**
     * 增加白盾 - 增加生命屏障
     */
    add_wall_hp(tag: baseBody, log: battle_logNum) {
        tag.P._wall_hp += log.val;
    }
    /**
     * 扣除白盾
     */
    del_wall_hp(tag: baseBody, log: battle_logNum) {
        log.type = EffectType.屏障;
        if (tag.P._wall_hp == 0) {
            log.val = 0;
            return;
        } else if (log.val >= tag.P._wall_hp) {
            log.val = tag.P._wall_hp;
            tag.P._wall_hp = 0;
        } else {
            let val = tag.P._wall_hp - log.val
            tag.P._wall_hp = tag.P._wall_hp - log.val;
            log.val = val
        }
    }
    /**
     * 恢复生命值
     */
    add_hp(tag: baseBody, log: battle_logNum) {
        if (tag.is_die()) {
            log.val = 0;
            return;
        }
        const val = log.val as number;
        if (tag.Hp_now + val >= tag._attributes.Hp_Max) {
            log.val = tag._attributes.Hp_Max - tag.Hp_now;
            tag.Hp_now = tag._attributes.Hp_Max
        } else {
            tag.Hp_now += val;
        }
    }
    /**
     * 恢复蓝量
     */
    add_mp(tag: baseBody, log: battle_logNum) {
        if (tag.is_die()) {
            log.val = 0;
            return;
        }
        const val = log.val as number;
        if (tag.Mp_now + val >= tag._attributes.Mp_Max) {
            log.val = tag._attributes.Mp_Max - tag.Hp_now;
            tag.Mp_now = tag._attributes.Mp_Max
        } else {
            tag.Mp_now += val;
        }
    }
    /**
   * 受到物理伤害
   */
    hit_Physical(tag: baseBody, log: battle_logNum, sk?: SKILL) {
        let hitVal = 0;
        // 伤害值 = 攻击 - 防御
        // hitVal = log.val * log.val / tag._attributes.Physical_defense;
        hitVal = log.val - tag._attributes.Physical_defense;
        if (hitVal <= 1) {
            hitVal = app.generateRandom(1, 10)
        }
        if (tag.Hp_now < hitVal) {
            hitVal = tag.Hp_now;
        }
        tag.Hp_now -= hitVal;
        log.val = hitVal;
        if (sk) {
            if (sk.minType == Sk_minType.普通攻击) {
                tag.sk_trigger(EffectCondition.auto_受到普通攻击时, [tag, sk._parent])
            }
        }
    }
    /**
* 受到魔法伤害
*/
    hit_Magic(tag: baseBody, log: battle_logNum) {
        let hitVal = 0;
        // 伤害值 = 攻击 - 防御
        // hitVal = log.val * log.val / tag._attributes.Physical_defense;
        hitVal = log.val - tag._attributes.Magic_defense;
        if (hitVal <= 1) {
            hitVal = app.generateRandom(1, 10)
        }
        if (tag.Hp_now < hitVal) {
            hitVal = tag.Hp_now;
        }
        tag.Hp_now -= hitVal;
        log.val = hitVal;
    }
    /**
     * 受到真实伤害
     */
    hit_read(tag:baseBody,log: battle_logNum){
        if(tag.Hp_now > log.val){
            tag.Hp_now -= log.val;
        }else{
            log.val = tag.Hp_now;
            tag.Hp_now -= log.val;
        }

    }
    /**
     * 伤害暴击转换
     */
    cover_Physical_crit(tag: baseBody, log: battle_logNum) {
        if (tag._attributes.Physical_crit >= 100) {
            log.val = log.val * (2 + tag._attributes.Physical_crit / 100)
        } else if (app.random(0, 100) < tag._attributes.Physical_crit) {
            try {
                log.val = log.val * 2
            } catch (error) {
                debugger
            }
        }
    }
    /**
  * 伤害暴击转换
  */
    cover_Magic_crit(tag: baseBody, log: battle_logNum) {
        if (tag._attributes.Magic_crit >= 100) {
            log.val = log.val * (2 + tag._attributes.Magic_crit / 100)
        } else if (app.random(0, 100) < tag._attributes.Magic_crit) {
            try {
                log.val = log.val * 2
            } catch (error) {
                debugger
            }
        }
    }
}
export default new body_tool()